<?php
namespace Application\Site1Bundle\DataSource;

class DatabaseDataSource implements \Bundle\ModuleBundle\Interfaces\DataSourceInterface
{
    private $entityManager;
    private $id;

    private $entity;

    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function setSourceId($id)
    {
        $this->id = $id;
    }

    public function getContent()
    {
        if($this->entity == null)
        {
            $this->entity = $this->entityManager->
                    find('Site1Bundle:Text', $this->id);
                    
        }

        return $this->entity->getText();
    }

    public function setText($text)
    {
               if($this->entity == null)
        {
            $this->entity = $this->entityManager->
                    find('Site1Bundle:Text', $this->id);

        }

        $this->entity->setText($text);
        $this->entityManager->persist($this->entity);
        $this->entityManager->flush($this->entity);
    }

}